package com.fruit.auth.web.action;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;

import com.fruit.auth.util.PrivilegeUtil;
import com.fruit.web.action.util.BaseAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.Interceptor;

@Component
public class PrivilegeInterceptor implements Interceptor
{

	private static final long serialVersionUID = -1069717507231593059L;

	@Override
	public void destroy()
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init()
	{
		// TODO Auto-generated method stub
		
	}

	@SuppressWarnings("rawtypes")
	@Override
	public String intercept(ActionInvocation invocation) throws Exception
	{
		BaseAction action=(BaseAction) invocation.getAction();
		ActionProxy proxy=invocation.getProxy();
		String actionName=proxy.getActionName();
		if((actionName.startsWith("jsps")
				&&!actionName.startsWith("jsps/securityJsp"))||
				actionName.equals("user/menus")
				|| action instanceof LoginAction
				||action instanceof IndexAction)
			return invocation.invoke();
		
		boolean authroizeResult=PrivilegeUtil.hasPrivilege(actionName, ServletActionContext.getRequest());
		return authroizeResult?invocation.invoke():"refused";
	}

}
